package hn.cch.job;

import hn.cch.constant.Path;
import org.apache.log4j.PropertyConfigurator;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class InitJob {



    private static Logger logger = LoggerFactory.getLogger(InitJob.class);

    /**
     *
     *
     *
     */

    private static String TestJobGroup = "TestJobGroup";
    private static String TestJobDetail = "TestJobDetail";
    private static String TestCronTrigger = "TestCronTrigger";
    private static String TestSimpleTrigger = "TestSimpleTrigger";

    // 调度器
    private static Scheduler scheduler = null;

    // 单例模式
    private static InitJob instance = null;

    public static InitJob getInstance(String jobPath) {
        if (instance != null){
            instance = new InitJob(jobPath);
        }
        return instance;
    }

    private InitJob(String jobPath) {
        try {
            if(scheduler != null){
                StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory(jobPath);
                scheduler = stdSchedulerFactory.getScheduler();
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }


    public void initAllJobs() {

        initTestJob();
    }

    private void  initTestJob() {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, 1);
        Date time = calendar.getTime();

        TimeZone timeZone = TimeZone.getDefault();
        String timeCron = "59 * * * * ?";

        int repeatCount = 1;
        long repeatInterval = 1;


        SimpleTrigger testSimpleTrigger = new SimpleTrigger(
            TestSimpleTrigger, TestJobGroup,
            TestJobDetail, TestJobGroup,
            date, time,
            repeatCount, repeatInterval
        );



        //触发
        try {
            CronTrigger testCronTrigger = new CronTrigger(
                TestCronTrigger, TestJobGroup,
                TestJobDetail, TestJobGroup,
                date, time,
                timeCron, timeZone);



            //任务
            JobDetail testJobDetail  = new JobDetail(TestJobDetail, TestJobGroup, TestJob.class);
            scheduler.scheduleJob(testJobDetail, testCronTrigger);

        } catch (ParseException e) {
            logger.error("corn parse error");
            e.printStackTrace();
        } catch (SchedulerException e) {
            logger.error("job schedule error");
            e.printStackTrace();
        }
    }




    public static void schedule(JobDetail jobDetail, Trigger trigger){

    }

    public static void reschedule(JobDetail jobDetail, Trigger trigger){

    }


    public static void main(String[] args) {
        PropertyConfigurator.configure(Path.LOG_PATH);
        // InitJob.getInstance(Path.JOB_PATH).initAllJobs();
    }
    public static void test() {

        try {

            Date date = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MINUTE, 1);
            Date time = calendar.getTime();

            TimeZone timeZone = TimeZone.getDefault();
            String timeCron = "59 * * * * ?";

            int repeatCount = 1;
            long repeatInterval = 1;











            //调度器
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

            //任务
            JobDetail testJobDetail  = new JobDetail(TestJobDetail, TestJobGroup, TestJob.class);

            //触发
            CronTrigger testCronTrigger = new CronTrigger(
                TestCronTrigger, TestJobGroup,
                TestJobDetail, TestJobGroup,
                date, time,
                timeCron, timeZone);

            SimpleTrigger testSimpleTrigger = new SimpleTrigger(
                TestSimpleTrigger, TestJobGroup,
                TestJobDetail, TestJobGroup,
                date, time,
                repeatCount, repeatInterval
            );


            //关联
            // scheduler.scheduleJob(testJobDetail, testCronTrigger);

            // //添加任务
            scheduler.addJob(testJobDetail, true);
            // //设置调度触发时间
            scheduler.scheduleJob(testCronTrigger);
            // //重设调度触发时间
            scheduler.rescheduleJob("TestCronTrigger", TestJobGroup, testSimpleTrigger);


            //启动
            scheduler.start();

            //关闭
            //scheduler.shutdown();

        } catch (SchedulerException | ParseException e) {
            e.printStackTrace();
        }
        System.out.println(TimeZone.getAvailableIDs());
    }

}


